// pages/edit_ge_ren_info/edit_ge_ren_info.js
import {ajax} from "../../utils/httpRequest.js";
import {URI_EDITUSERINFO, URI_GETBASEINFO,URI_MINGZUILIST} from "../../utils/api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: '',
    education: '',
    information: {
      date: '',
      sexinfo: {
        sexindex: '',
        sexarray: ['男', '女']
      },
      minority: {
        minorityindex: '',
        minorityarray: []
      },
      education: {
        educationindex: '',
        //educationarray: ['', '', '', '', '','','','']
        educationarray: ['小学', '初中', '高中', '专科', '本科', '硕士', '博士', '未知']
      },
      active: true
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMzList();
  },

  getBaseInfo() {
    ajax.get(URI_GETBASEINFO, {
      dangyaun_id: wx.getStorageSync('dangyuan_id')
    }, {}, res => {
      this.setData({
        'information.username': res.data.section3.name,
        'information.useremail': res.data.section3.email,
        'information.usercard': res.data.section3.user_card,
        'information.date': res.data.section3.birthday,
        'information.userposition': res.data.section3.post,
        'information.gonghao':res.data.section3.job_number,
        sex:res.data.section3.sex
      });
      if (res.data.section3.edu == 0) {
        this.setData({
          'information.education.educationindex': 7
        });
      } else {
        this.setData({
          'information.education.educationindex': res.data.section3.edu - 1 + ''
        });
      }
      this.data.information.minority.minorityarray.forEach((value, index) => {
        if (value.mzname == res.data.section3.mzname) {
          this.setData({
            'information.minority.minorityindex': index + ''
          })
        }
      });
      if (res.data.section3.sex == 1) {
        this.setData({
          'information.sexinfo.sexindex': '0',
        })
      } else {
        this.setData({
          'information.sexinfo.sexindex': '1',
        })
      }

    })
  },

  getMzList(){
    ajax.get(URI_MINGZUILIST,{},{},res=>{
      this.setData({
        'information.minority.minorityarray': res.data
      });
      this.getBaseInfo();
    });
  },


  // 信息录入部分开始 input onblur 事件
  getUserName: function (e) {

  },
  getUserEmail: function (e) {

  },
  getUserCard: function (e) {

  },
  getUserPosition: function (e) {

  },


  handleClick() {
    const addCurrent = this.data.currentindex + 1;
    const currentindex = addCurrent > 2 ? 0 : addCurrent;
    this.setData({
      'currentindex': currentindex
    })
  },

  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'information.date': e.detail.value
    })
  },
  bindSexChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'information.sexinfo.sexindex': e.detail.value,
      sex: parseInt(e.detail.value)+1
    })
  },
  bindMinorityChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'information.minority.minorityindex': e.detail.value
    })
  },
  bindEducationChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'information.education.educationindex': e.detail.value
    });
    if (e.detail.value <= 6) {
      this.setData({
        education: parseInt(e.detail.value) + 1
      })
    } else {
      this.setData({
        education: 0
      })
    }
  },
  submitinfo(e) {
    if (this.data.information.active) {
      let formData = e.detail.value;
      console.log(formData);
      var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
      let regEmail = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
      if (!formData.username) {
        this.showMessage('请输入您的真实名字');
        return false;
      } else if (!formData.useremail) {
        this.showMessage('请输入您的邮箱');
        return false;
      } else if (!regEmail.test(formData.useremail)) {
        this.showMessage('请输入正确邮箱');
        return false
      } else if (!formData.usercard) {
        this.showMessage('请输入您的身份证号');
        return false
      } else if (!regIdCard.test(formData.usercard)) {
        this.showMessage('您输入的身份证不正确！');
        return false
      } else if (!formData.userdate) {
        this.showMessage('请选择出生日期');
        return false
      } else if (!formData.sexinfo) {
        this.showMessage('请选择性别');
        return false;
      } else if (!formData.minority) {
        this.showMessage('请选择所在名族');
        return false;
      } else if (!formData.education) {
        this.showMessage('请选择您的学历');
        return false;
      } else if (!formData.userposition) {
        this.showMessage('请输入您的工作岗位');
        return false;
      }
      ajax.post(URI_EDITUSERINFO, {
        id: wx.getStorageSync('dangyuan_id'),
        name: formData.username,
        email: formData.useremail,
        user_card: formData.usercard,
        birthday: formData.userdate,
        sex: parseInt(this.data.information.sexinfo.sexindex)+1,
        mz_id: formData.minority,
        edu: this.data.information.education.educationindex <= 6 ? parseInt(this.data.information.education.educationindex) + 1 : '0',
        post: formData.userposition,
        job_number:formData.gonghao
      }, {}, res => {
        wx.showModal({
          title: '温馨提示',
          content: '个人信息已修改成功',
          showCancel: false,
          confirmText: "确定",
          confirmColor: '#0d77e4',
          success: (res) => {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      })
    } else {
      console.log('您还没有输入信息');
    }
  },
  showMessage(text) {
    wx.showToast({
      title: text,
      icon: 'none',
      duration: 2000
    })
  },

  // 信息录入部分结束

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})